package com.makeunion.gdlbsdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MultiPointItem;
import com.amap.api.maps.model.MultiPointOverlay;
import com.amap.api.maps.model.MultiPointOverlayOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.busline.BusLineItem;
import com.amap.api.services.busline.BusLineQuery;
import com.amap.api.services.busline.BusLineResult;
import com.amap.api.services.busline.BusLineSearch;
import com.amap.api.services.busline.BusStationItem;
import com.amap.api.services.busline.BusStationQuery;
import com.amap.api.services.busline.BusStationResult;
import com.amap.api.services.busline.BusStationSearch;
import com.amap.api.services.core.LatLonPoint;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class SearchBusActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    private BusStationSearch busStationSearch;

    private BusLineSearch busLineSearch;

    private MultiPointOverlay multiPointOverlay;

    private Polyline polyline;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_bus);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    public void searchBusStation(View view) {
        BusStationQuery busStationQuery = new BusStationQuery("望京站", "010");
        busStationSearch = new BusStationSearch(this, busStationQuery);
        busStationSearch.setOnBusStationSearchListener(
            new BusStationSearch.OnBusStationSearchListener() {
                @Override
                public void onBusStationSearched(BusStationResult busStationResult, int i) {
                    if (busStationResult == null) {
                        return;
                    }
                    List<BusStationItem> busStations = busStationResult.getBusStations();
                    if (busStations == null) {
                        return;
                    }
                    List<MultiPointItem> list = new ArrayList<>();
                    for (BusStationItem item : busStations) {
                        LatLonPoint point = item.getLatLonPoint();
                        list.add(new MultiPointItem(new LatLng(point.getLatitude(), point.getLongitude())));
                    }
                    showResultOnMap(list);
                }
            });
        busStationSearch.searchBusStationAsyn();
    }

    public void searchBusLine(View view) {
        BusLineQuery busLineQuery = new BusLineQuery("854", BusLineQuery.SearchType.BY_LINE_NAME, "010");
        busLineQuery.setPageSize(20);
        busLineQuery.setPageNumber(0);
        busLineSearch = new BusLineSearch(this,busLineQuery);
        busLineSearch.setOnBusLineSearchListener(new BusLineSearch.OnBusLineSearchListener() {
            @Override
            public void onBusLineSearched(BusLineResult busLineResult, int i) {
                if (busLineResult == null) {
                    return;
                }
                List<BusLineItem> busLines = busLineResult.getBusLines();
                if (busLines == null) {
                    return;
                }

                List<MultiPointItem> list = new ArrayList<>();
                List<LatLng> latlngList = new ArrayList<>();
                BusLineItem busLine = busLines.get(0);
                List<BusStationItem> busStations = busLine.getBusStations();
                for (BusStationItem station : busStations) {
                    LatLonPoint point = station.getLatLonPoint();
                    LatLng latLng = new LatLng(point.getLatitude(), point.getLongitude());
                    latlngList.add(latLng);
                    list.add(new MultiPointItem(latLng));
                }
                showLineOnMap(latlngList);
                showResultOnMap(list);
            }
        });
        busLineSearch.searchBusLineAsyn();
    }

    private void showResultOnMap(List<MultiPointItem> list) {
        if (multiPointOverlay != null) {
            multiPointOverlay.remove();
        }
        MultiPointOverlayOptions overlayOptions = new MultiPointOverlayOptions();
        overlayOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_blue_dot));
        overlayOptions.anchor(0.5f,0.5f);

        multiPointOverlay = aMap.addMultiPointOverlay(overlayOptions);
        multiPointOverlay.setItems(list);

        AMap.OnMultiPointClickListener multiPointClickListener = new AMap.OnMultiPointClickListener() {
            @Override
            public boolean onPointClick(MultiPointItem pointItem) {
                Log.e("TAG", "pointItem=" + pointItem.getLatLng());
                Toast.makeText(SearchBusActivity.this, "pointItem=" + pointItem.getLatLng(), Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        aMap.setOnMultiPointClickListener(multiPointClickListener);
    }

    private void showLineOnMap(List<LatLng> list) {
        if (polyline != null) {
            polyline.remove();
        }
        polyline = aMap.addPolyline((new PolylineOptions())
                .addAll(list)
                .width(8)
                .setDottedLine(false)
                .color(Color.RED));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
